#include "globalvar2.h"

#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_io.h"  
#include "hi_gpio.h" 
#include "hi_pwm.h"
#include "iot_gpio.h"
#include "iot_errno.h"

#define     IOT_GPIO_IDX_9          9
#define     IOT_GPIO_PWM_FUNCTION   5
#define     IOT_PWM_PORT_PWM0       0

void Firealarm_Gpio_Init(void)
{
    hi_gpio_init();
    IoTGpioInit(IOT_GPIO_IDX_9);
    hi_io_set_func(IOT_GPIO_IDX_9, IOT_GPIO_PWM_FUNCTION);
    IoTGpioSetDir(IOT_GPIO_IDX_9, IOT_GPIO_DIR_OUT);
    IoTPwmInit(IOT_PWM_PORT_PWM0);
}

void Beep_switch(int flag)
{
    if(flag == 1)
    {
        IoTPwmStart(IOT_PWM_PORT_PWM0, 50, 4000);
        printf("  !!***************!!\n");
		printf("  !!    firealarm  !!\n");
		printf("  !!***************!!\r\n");
    }
    if(flag == 0)
    {
        IoTPwmStop(IOT_PWM_PORT_PWM0);
        printf("  !!***************!!\n");
		printf("  !!    firestopalarm  !!\n");
		printf("  !!***************!!\r\n");
    }
}

void Firealarm_task(void)
{
    while(1)
    {
        if(g_temp > 30 && g_gas < 10 && g_gas > 0)
        {
            Beep_switch(1);
        }
        else
        {
            Beep_switch(0);
            // g_fire_alarm_status = 0;
        }
        usleep(1 * 1000 * 1000);
    }
}

void Firealarm_entry(void)
{
    Firealarm_Gpio_Init();
	usleep(3 * 1000 * 1000); //延时3s
	osThreadAttr_t attr; //任务属性结构体
	attr.name = "Firealarm_task"; //任务名称
	attr.stack_size = 1024; //任务内存大小
	attr.priority = 25;
    if(osThreadNew((osThreadFunc_t)Firealarm_task, NULL, &attr) == NULL)
    {
        printf("[Firealarm] Falied to create Firealarm!\n");
    }
    printf("[Firealarm] Succeed to create Firealarm!\n");
}

SYS_RUN(Firealarm_entry);